//
//  JLHomeViewController.m
//  Liang美团HD
//
//  Created by sun on 16/1/19.
//  Copyright © 2016年 sun. All rights reserved.
//
#import "JLSort.h"
#import "JLHomeViewController.h"
#import "DPAPI.h"
#import "UIBarButtonItem+JLBarButtonItem.h"
#import "JLHomeTopItem.h"
#import "JLSortViewController.h"
#import "JLCategoryController.h"
#import "JLCategories.h"
#import "JLCityViewController.h"
#import "JLDistrictViewController.h"
#import "JLCities.h"
#import "JLData.h"
#import "JLDistricts.h"
@interface JLHomeViewController ()
@property(nonatomic,weak)UIBarButtonItem * categoryTopItem;
@property(nonatomic,weak)UIBarButtonItem * districttopitem;
@property(nonatomic,weak)UIBarButtonItem * sortTopItem;
@property(nonatomic,strong)JLCities * currentCitiy;
@property(nonatomic,strong)JLDistrictViewController * districtVc;
@end

@implementation JLHomeViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.backgroundColor = JLColor(224, 224, 224, 1);

    [self setupNavLeft];
    [self setupNavRight];
    [self setUpNotifcation];
    
    //设置默认城市为北京
    JLHomeTopItem * item = self.districttopitem.customView;
    self.currentCitiy = [JLData searchCitiesWithName:item.text];
    
}
-(void)setUpNotifcation{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sortNote:) name:JLSortButtonDidClickNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(categoryNotification:) name:JLCategoryButtonDidClickNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cityNotification:) name:JLCityDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(districtsNotification:) name:JLDistrictsDidChangeNotification object:nil];
}
-(void)districtsNotification:(NSNotification *)notification{
    JLDistricts * districts = notification.object;
    NSUInteger row = [notification.userInfo[JLDistrictsIndexFromTableViewDidClickKey] intValue];
    NSString * subdistrict = districts.subdistricts[row];
     JLHomeTopItem * item = self.districttopitem.customView;
    item.text = [NSString stringWithFormat:@"%@ | %@",self.currentCitiy.name,districts.name];
    if (subdistrict){
    item.subText = [NSString stringWithFormat:@"%@",subdistrict];
    }else{
    item.subText = @"";
    }
}
-(void)cityNotification:(NSNotification *)notification{
    JLHomeTopItem * item = self.districttopitem.customView;
    item.text = [notification.object name];
    item.subText = nil;
    self.currentCitiy = notification.object;
}

-(void)sortNote:(NSNotification *)note{
    JLSort * sort = note.object;
      JLHomeTopItem * item =  self.sortTopItem.customView;
    item.subText = sort.label;
#warning TODO重新发送请求给服务器
}
-(void)categoryNotification:(NSNotification *)noti{
    JLHomeTopItem * item = self.categoryTopItem.customView;
    
        int row = [noti.userInfo[JLCategoryIndexFromButtonDidClickKey] intValue];
        JLCategories * cate = noti.object;
        item.Text = cate.name;
      item.subText = cate.subcategories[row];
#warning TODO重新发送请求给服务器
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)setupNavLeft{
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_meituan_logo"] style:UIBarButtonItemStylePlain target:nil action:nil];
    //分类按钮
    JLHomeTopItem * categoryTop = [JLHomeTopItem itemWithTitle:@"全部分类" subTitle:nil norImage:@"icon_category_-1"highImage:@"icon_category_highlighted_-1" target:self action:@selector(categoryClick)];
    UIBarButtonItem * categoryTopItem = [[UIBarButtonItem alloc]initWithCustomView:categoryTop];
    self.categoryTopItem = categoryTopItem;
    //区域
    JLHomeTopItem * districtTop = [JLHomeTopItem itemWithTitle:@"北京" subTitle:@"海淀" norImage:@"icon_district"highImage:@"icon_district_highlighted" target:self action:@selector(districttopClick)];
    UIBarButtonItem * districttopitem = [[UIBarButtonItem alloc]initWithCustomView:districtTop];
    self.districttopitem = districttopitem;
    //排序
    JLHomeTopItem * sortTop = [JLHomeTopItem itemWithTitle:@"排序" subTitle:@"默认排序" norImage:@"icon_sort"highImage:@"icon_sort_highlighted" target:self action:@selector(sortTopClick)];
    UIBarButtonItem * sortTopItem = [[UIBarButtonItem alloc]initWithCustomView:sortTop];
    self.sortTopItem = sortTopItem;
    
    self.navigationItem.leftBarButtonItems = @[item,categoryTopItem,districttopitem,sortTopItem];
}
-(void)categoryClick{
    JLCategoryController * cateVc = [[JLCategoryController alloc]init];
    cateVc.modalPresentationStyle = UIModalPresentationPopover;
    cateVc.popoverPresentationController.barButtonItem = self.categoryTopItem;
    [self presentViewController:cateVc animated:YES completion:nil];
}
-(void)districttopClick{
    JLDistrictViewController * districtVc = [[JLDistrictViewController alloc]init];
    districtVc.modalPresentationStyle = UIModalPresentationPopover;
    districtVc.popoverPresentationController.barButtonItem = self.districttopitem;
    self.districtVc = districtVc;
    
    self.districtVc.districts = self.currentCitiy.districts;
    
    [self presentViewController:districtVc animated:YES completion:nil];
}
-(void)sortTopClick{
    JLSortViewController * sortVc = [[JLSortViewController alloc]init];
    sortVc.modalPresentationStyle = UIModalPresentationPopover;
    sortVc.popoverPresentationController.barButtonItem = self.sortTopItem;
    [self presentViewController:sortVc animated:YES completion:nil];
}
-(void)setupNavRight{
    UIBarButtonItem * search = [UIBarButtonItem itemWithnorImage:@"icon_search" highImage:@"icon_search_highlighted" target:self action:@selector(searchItemClick)];
    search.customView.width = 50;
    UIBarButtonItem * map = [UIBarButtonItem itemWithnorImage:@"icon_map" highImage:@"icon_map_highlighted" target:self action:@selector(mapItemClick)];
    self.navigationItem.rightBarButtonItems = @[search,map];
    map.customView.width = 50;
}
-(void)searchItemClick{
    
}
-(void)mapItemClick{
    
}
#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 0;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
